// time.js
import { Request } from "../../utils/request.js"
var util = require('../../utils/util.js');
var request = new Request()
const app = getApp()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    //事项列表
    eventList: [],
    //结束模态框显示状态
    EndModal: false,
    //计时按钮状态
    isClock: false,
    //计时器时间
    hour: '0' + 0,   // 时
    minute: '0' + 0,   // 分
    second: '0' + 0,    // 秒
    //计时器句柄
    timer: 0,
    //结束计时表单内容
    rate: 5,
    item: '',
    studyTime: 0,
    effectiveStudyTime: 0,
    //顶部时间
    date: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      date : util.eMonth() + " " + util.nDay()
    })
  },


  onShow: function (options) {
    console.log(app.globalData.openid);
    console.log(wx.getStorageSync('openId'));
    if(!app.globalData.openid){
      return;
    }
    
    var that = this
    console.log(request)
    //获取用户计时状态
    request.get(
      'timeope/' + app.globalData.openid,
      null,
      function (res) {
        console.log(res);
        if(res.statusCode != 200){
          return ;
        }
        if (!res.data.result.status) {
          that.setData({
            isClock: false
          });
        } else {
          that.setData({
            isClock: true
          });
          that.startTimer()
          //这里没有设置时间
        }
      },
      function (res) {
        console.log(res);
      }
    );
    //获取用户历史事项列表
    request.get(
      "getEventList/" + app.globalData.openid,
      null,
      (res) => {
        console.log(res)
        if(res.statusCode != 200){
          return ;
        }
        that.setData({
          eventList: res.data.result.eventlist
        });
      }
    )
  },

  /*
  * 开启文字计时器
  */
  startTimer: function () {
    const that = this
    var second = that.data.second
    var minute = that.data.minute
    var hours = that.data.hour
    var nTimer = setInterval(function () {  // 设置定时器
      second++
      if (second >= 60) {
        second = 0  //  大于等于60秒归零
        minute++
        if (minute >= 60) {
          minute = 0  //  大于等于60分归零
          hours++
          if (hours < 10) {
            // 少于10补零
            that.setData({
              hour: '0' + hours
            })
          } else {
            that.setData({
              hour: hours
            })
          }
        }
        if (minute < 10) {
          // 少于10补零
          that.setData({
            minute: '0' + minute
          })
        } else {
          that.setData({
            minute: minute
          })
        }
      }
      if (second < 10) {
        // 少于10补零
        that.setData({
          second: '0' + second
        })
      } else {
        that.setData({
          second: second
        })
      }
    }, 1000)
    that.setData({
      timer: nTimer
    })
  },

  /*
  * 显示结束模态框
  */
  showEndModal(e) {
    if(!app.globalData.openid){
      wx.switchTab({
        url: "../user/user",
      })
    }
    var that = this
    request.get(
      'addRecordingFake/' + app.globalData.openid,
      null,
      (res) => {
        that.setData({
          'studyTime': res.data.result.studytime,
          'effectiveStudyTime': res.data.result.effectivetime
        });
      }
    )
    this.setData({
      EndModal: true
    })
  },

  /*
  * 隐藏结束模态框
  */
  hideEndModal(e) {
    this.setData({
      EndModal: false
    })
  },


  /*
  * 开始计时
  */
  startClock(e) {
    let that = this;
    if(app.globalData.openid){
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            request.get(
              "addRecordingBegin/" + app.globalData.openid,
              null,
              function (res) {
                that.setData({
                  isClock: true,
                  hour: '0' + 0,
                  minute: '0' + 0,
                  second: '0' + 0
                })
                that.startTimer(0, 0, 0)
              }
            );
          }else{
            wx.switchTab({
              url: "../user/user",
            })
          }
        }
      })
    } else {
      wx.switchTab({
        url: "../user/user",
      })
    }
  },

  /*
  * 结束计时
  */
  endClock(e) {
    if(!app.globalData.openid){
      wx.switchTab({
        url: "../user/user",
      })
    }
    var that = this
    //添加记录
    request.post(
      "addRecordingEnd",
      {
        openid: app.globalData.openid,
        comment: this.data.rate,
        event: this.data.item
      },
      function (res) {
        console.log(res)
          //获取用户历史事项列表
          request.get(
            "getEventList/" + app.globalData.openid,
            null,
            (res) => {
              console.log(res)
              if(res.statusCode != 200){
                return ;
              }
              that.setData({
                eventList: res.data.result.eventlist
              });
            }
          )
      }
    )

    if (this.timer != 0) {
      clearInterval(this.data.timer)
    }
    this.hideEndModal(e)
    this.setData({
      isClock: false
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //评分控件change event
  rateChange: function (value) {
    this.setData({
      rate: value.detail
    })
  },

  //事项input change event
  itemChange: function (e) {
    this.setData({
      item: e.detail.value
    });
  }
})